package net.loodies;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class LoodiesFixedViewLoader extends Thread {
	LoodiesFixedViewLoaderCallback callback;
	String url;
	public String JSONString;
	Activity _Activity;
	Bitmap bitmap;
	String loodieType;
	LoodiesFixedView view;

	public LoodiesFixedViewLoader(String url,
			LoodiesFixedViewLoaderCallback callback, LoodiesFixedView view,
			Activity _Activity) {
		this.url = url;
		this.callback = callback;
		this.view = view;
		this._Activity = _Activity;

		start();
	}

	public void run() {
		try {
			// TODO le JSON dans utils
			JSONString = Utils.loadJSON(url);
			try {
				LoodiesParameters.map = Utils.parse(JSONString);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (LoodiesParameters.map != null) {
				loodieType = LoodiesParameters.map.get("type");
				// System.out.println(loodieType);
			}
			if (LoodiesParameters.map != null) {

				String bitmapUrlStr = LoodiesParameters.map.get("ad_object_url");
				if (bitmapUrlStr != null) {
					// bitmap = LoodiesUtils.downloadFile(urlstr);

					URL bitmapUrl = null;
					try {
						bitmapUrl = new URL(bitmapUrlStr);
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					HttpGet httpRequest = null;

					try {
						httpRequest = new HttpGet(bitmapUrl.toURI());
					} catch (URISyntaxException e) {
						e.printStackTrace();
					}

					HttpClient httpclient = new DefaultHttpClient();
					HttpResponse response = null;
					try {
						response = (HttpResponse) httpclient
						.execute(httpRequest);
					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						return;
					}

					HttpEntity entity = response.getEntity();
					BufferedHttpEntity bufHttpEntity = null;
					try {
						bufHttpEntity = new BufferedHttpEntity(entity);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					try {
						InputStream instream = bufHttpEntity.getContent();
						bitmap = BitmapFactory.decodeStream(instream);
						// return _bitmap;
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					ImageDisplayer displayer = new ImageDisplayer(view, bitmap);
					_Activity.runOnUiThread(displayer);

				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		callback.LoodiesFixedViewLoaded();
	}
}
